<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Geoip Service - Maxmind driver
 *
 * $Id$
 *
 * @package    Core
 * @author     Sam Clark
 * @copyright  (c) 2008 Polaris Digital
 * @license    GNU Public Licence v3
 */
class Geoip_Maxmind_Driver extends Geoip_Driver {

	/**
	 * Query function to perform
	 * @return 
	 * @param $ip Object[optional]
	 */
	public function query( $ip = FALSE )
	{
		// Setup result
		$result = FALSE;
		
		// Remove whitespace from the result
		$ip = trim( $ip );
		
		// If there is an IP 
		if( $ip !== FALSE and $this->validate_ip( $ip ) )
		{
			// Setup the CURL url to call
			$url = "http://geoip1.maxmind.com/b?l={$this->license_key}&i={$ip}";
			
			// Create the CURL object
			$query = curl_init( $url );
			
			// Setup the CURL object to output the result to a variable
			curl_setopt( $query, CURLOPT_RETURNTRANSFER, 1 );
			
			// Execute the CURL request
			$this->response_cache = curl_exec( $query );
			
			// Setup the result
			$result = new Maxmind_Result( $this->response_cache );
		}
		
		// Return the result
		return $result;
	}

}

/**
 * Resulting Maxmind data result object
 */
class Maxmind_Result implements Geoip_Result
{
	// Plain text result
	protected $result;
	
	// Keys for getting / setting values
	protected $keys = array(
		'countrycode',
		'regioncode',
		'city',
		'latitude',
		'longitude',
		'errorcode'
	);
	
	// Data store
	protected $data = array();
	
	
	/**
	 * Overloading the constructor
	 * @return 
	 * @param $output Object[optional]
	 */
	public function __construct( $output = FALSE )
	{
		// Setup result
		$result = FALSE;
		
		// IF there is some output
		if( $output !== FALSE )
		{
			// Set that to the model result
			$this->result = $output;
			
			// Parse the results
			$result = $this->format_results();
			
		}
		// Return the result of the process
		return $result;
	}
	
	/**
	 * Overload the __get() method to retrieve the data
	 * from the key supplied
	 * @return 
	 * @param $key Object
	 */
	public function __get( $key )
	{
		// Setup the result
		$result = FALSE;
		
		// If key isn't data and the key exists
		if( $key !== 'data' and array_key_exists( $key, $this->data ) )
		{
			// Setup the result
			$result = $this->data[ $key ];
		}
		
		// Return the result
		return $result;
	}
	
	/**
	 * Format the resulting data
	 * @return (bool) TRUE on success, FALSE on fail
	 */
	public function format_results()
	{
		// Setup the result
		$result = FALSE;
		
		// If the result isn't empty and has a comma
		if( strlen( $this->result ) > 0 and strpos( $this->result, ',' ) !== FALSE )
		{
			// Turn the string into an array
			$output_array = explode( ',', trim( $this->result ) );
			
			// Process the resulting array into the model
			foreach( $output_array as $key => $val )
			{
				// Overload the data field with the resulting data
				$this->data[ $this->keys[$key] ] = $val;
			}
			// Setup the new success result
			$result = TRUE;
		}
		// Return the result
		return $result;
	}
	
	/**
	 * Returns this object as an array
	 * @return 
	 */
	public function as_array()
	{
		return $this->data;
	}
	
}
